//
//  NetWorkError.m
//  CarService
//
//  Created by sixarm on 2018/5/5.
//  Copyright © 2018年 sixarm. All rights reserved.
//

#import "NetWorkError.h"
#import "XHToast.h"
#import "LBActivityView.h"

@implementation NetWorkError

SingletonM(Error)

/** auth 认证 非200 错误 */
- (long)authHTTPCode:(NSURLSessionTask *)task {
    [self hideLoading];
    long code ;
    NSHTTPURLResponse * responses = (NSHTTPURLResponse *)task.response;
    code = (long)responses.statusCode;

    if (code == 200) {
        return code;
    }
    if (code == 400) {
//        [XHToast showCenterWithText:@"用户名或者密码错误"];
        return code;
    }
    if (code == 401) {
//        [XHToast showCenterWithText:@"验证码错误或者已过期"];
        return code;
    }
    return 0;
}
- (void)handleRequestError:(NSError *)error {


    [self hideLoading];
    NSDictionary *errInfo = error.userInfo;
    NSString *errString = error.localizedFailureReason;
    NSString *NSLocalizedDescription = [errInfo objectForKey:@"NSLocalizedDescription"];
    DLog(@"error -- %ld ---- %@ --- %@",(long)error.code, errString, NSLocalizedDescription);
    NSData *data = [error.userInfo objectForKey:@"com.alamofire.serialization.response.error.data"];
    NSString *errString2 = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    DLog(@"com.alamofire.serialization.response.error.data ----_+_+%@+_+%@", data, errString2);

    if (error.code == -1001) {//请求超时情况的处理

        // [SVProgressHUD showErrorWithStatus:NSLocalizedDescription];
    }
    if (error.code == -1004) {//无法连接到服务器

        // [SVProgressHUD showErrorWithStatus:NSLocalizedDescription];

    }
    if (error.code == -1011) {//连接收到无效的服务器响应

        // [SVProgressHUD showErrorWithStatus:NSLocalizedDescription];
    }
    if (error.code == -2102) {//请求超时

        // [SVProgressHUD showErrorWithStatus:NSLocalizedDescription];
    }
    if (error.code == 3840) {//返回数据格式不正确

        // [SVProgressHUD showErrorWithStatus:errString];
    }
}
/**  各种错误40x 系列  */

- (int)filterError40Xstring:(id)response {

    //DLog(@"原始返回结果是 -- :%@",response)
    NSString *tmpStr  = [(NSDictionary *)response objectForKey:@"code"];
    //NSString *access_token  = [(NSDictionary *)response objectForKey:@"access_token"];
    if(tmpStr) { ///< -- 非token返回 -->
        int code = [tmpStr intValue];
        // -- > 正常接口通过
        switch (code) {
            case 0:
            {
                return code;
            }
                break;
            case 400:
            {
                NSString *msg  = [(NSDictionary *)response objectForKey:@"msg"];
                [XHToast showCenterWithText:msg];
                return 100000;
            }
                break;
            case 401:
            {
                NSString *msg  = [(NSDictionary *)response objectForKey:@"msg"];
                [XHToast showCenterWithText:msg];
                return 100000;
            }
                break;
            case 403:
            {
                NSString *msg  = [(NSDictionary *)response objectForKey:@"msg"];
                [XHToast showCenterWithText:msg];
                return 100000;
            }
                break;
            case 404:
            {
                NSString *msg  = [(NSDictionary *)response objectForKey:@"msg"];
                [XHToast showCenterWithText:msg];
                return 100000;
            }
                break;
            case 500:
            {
                NSString *msg  = [(NSDictionary *)response objectForKey:@"msg"];
                [XHToast showCenterWithText:msg];
                return 100000;
            }
                break;

            default:
                break;
        }
    }
    return 100000;
}
# pragma mark loading

- (void)showLoading {
    [LBActivityView wj_showActiviTy];
}

- (void)hideLoading {
    [LBActivityView wj_hideActiviTy];
}
@end
